package com.ygu.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ygu.ecare.utils.HTTPGetTool;
import com.ygu.ecare.utils.URLUtils;
import com.ygu.ecare.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ChooseArea extends Activity {
	private ProgressDialog prodialog = null;
	private List<Map<String,Object>> provincelist = new ArrayList<Map<String,Object>>();
	private List<Map<String,Object>> citylist = new ArrayList<Map<String,Object>>();
	private ListView province = null;
	private ListView city = null;
	private ImageButton back;
	private String pid = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.choosecity);
		//运行加载省份信息方法
		province = (ListView) findViewById(R.id.province);
		city = (ListView) findViewById(R.id.city);
		back = (ImageButton) findViewById(R.id.back);
		loadProvince();
		//调用设置监听的方法
		setOnClick();
	}
	//集中设置按钮监听
	private void setOnClick(){
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		province.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				TextView dqdm = (TextView) view.findViewById(R.id.dqdm);
				pid = dqdm.getText().toString();
				loadCityData();
			}
		});
		city.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				TextView cname = (TextView) view.findViewById(R.id.dqmc);
				TextView cid = (TextView) view.findViewById(R.id.dqdm);
				Intent intent = getIntent();
				intent.putExtra("cname", cname.getText().toString());
				intent.putExtra("cid", cid.getText().toString());
				ChooseArea.this.setResult(1, intent);
				ChooseArea.this.finish();
			}
			
		});
	}
	//加载省份信息的方法
	private void loadProvince() {
		prodialog = new ProgressDialog(ChooseArea.this);
		prodialog.setMessage("加载中");
		prodialog.show();
		//启动初始化省份信息的线程
		new Thread(new IniProvince()).start();
	}
	//加载城市信息
	private void loadCityData(){
		prodialog = new ProgressDialog(ChooseArea.this);
		prodialog.setMessage("加载中");
		prodialog.show();
		new Thread(new IniCity()).start();
	}
	// 开启线程加载省份信息
	class IniProvince implements Runnable {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			Message msg = myHandler.obtainMessage();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("pid", "0"));
			JSONObject provincejson = HTTPGetTool.getTool().post(
					URLUtils.HOSTMAIN + URLUtils.GETDQLIST, params);
			JSONArray provincejsonarray= null;
			try {
				provincejsonarray = provincejson.getJSONArray("dqinfo");
				JSONObject jobj = null;
				List<Map<String, Object>> provincetemp = new ArrayList<Map<String, Object>>();
				for (int i = 0; i < provincejsonarray.length(); i++) {
					Map<String, Object> provincemap = new HashMap<String, Object>();
					jobj = provincejsonarray.getJSONObject(i);
					provincemap.put("id", jobj.getString("id"));
					provincemap.put("dqmc", jobj.getString("dqmc"));
					provincetemp.add(provincemap);
				}
				msg.what = 1;
				msg.obj = provincetemp;
				myHandler.sendMessage(msg);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	class IniCity implements Runnable {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			Message msg = myHandler.obtainMessage();
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("pid", pid));
			JSONObject cityjson = HTTPGetTool.getTool().post(URLUtils.HOSTMAIN + URLUtils.GETDQLIST, params);
			JSONArray cityjsonarray= null;
			try {
				cityjsonarray = cityjson.getJSONArray("dqinfo");
				JSONObject jobj = null;
				List<Map<String, Object>> citytemp = new ArrayList<Map<String, Object>>();
				for (int i = 0; i < cityjsonarray.length(); i++) {
					Map<String, Object> citymap = new HashMap<String, Object>();
					jobj = cityjsonarray.getJSONObject(i);
					citymap.put("dqid", jobj.getString("id"));
					citymap.put("dqmc", jobj.getString("dqmc"));
					citytemp.add(citymap);
				}
				msg.what = 2;
				msg.obj = citytemp;
				myHandler.sendMessage(msg);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	// 处理加载信息
	Handler myHandler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch(msg.what){
			case 1:
				prodialog.dismiss();
				provincelist = (List<Map<String, Object>>) msg.obj;
				SimpleAdapter myprovinceadpter = new SimpleAdapter(ChooseArea.this.getApplicationContext(), provincelist, R.layout.province_data, new String[]{"id","dqmc"}, new int[]{R.id.dqdm,R.id.dqmc});
				province.setAdapter(myprovinceadpter);
				break;
			case 2:
				prodialog.dismiss();
				citylist = (List<Map<String, Object>>) msg.obj;
				SimpleAdapter mycityadapter = new SimpleAdapter(ChooseArea.this.getApplicationContext(), citylist, R.layout.data_city,new String[]{"dqid","dqmc"}, new int[]{R.id.dqdm,R.id.dqmc});
				city.setAdapter(mycityadapter);
				break;
			}
		}
	};
}
